package com.lu.statistics.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lu.statistics.common.config.ServerResponse;
import com.lu.statistics.common.constant.ResponseCode;
import com.lu.statistics.common.utils.LoggerUtils;
import com.lu.statistics.entity.RCustomer;
import com.lu.statistics.entity.front.request.CreateCustomer;
import com.lu.statistics.entity.front.request.EditCustomer;
import com.lu.statistics.entity.front.response.CustomerResponse;
import com.lu.statistics.service.RCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(value = "客户模块", tags = {"客户模块"})
@RestController
@RequestMapping("/api/customer")
public class RCustomerController {
    @Autowired
    private RCustomerService customerService;

    @ApiOperation(value = "添加客户", notes = "添加客户")
    @PostMapping(value = "/addCustomer")
    private ServerResponse<String> addCustomer(@RequestBody CreateCustomer createCustomer) {
        try {
            RCustomer customer = new RCustomer();
            BeanUtils.copyProperties(createCustomer, customer);
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            customer.setCreateId(userId);
            customerService.saveOrUpdateCustomer(customer);
        } catch (DuplicateKeyException e) {
            LoggerUtils.error(getClass(), "[rCustomer addCustomer]", e);
            return ServerResponse.error(ResponseCode.CUSTOMER_EXISTS);
        }
        return ServerResponse.success();
    }

    @ApiOperation(value = "编辑客户", notes = "编辑客户")
    @PostMapping(value = "/editCustomer")
    private ServerResponse<String> editCustomer(@RequestBody EditCustomer editCustomer) {

        try {
            RCustomer customer = new RCustomer();
            BeanUtils.copyProperties(editCustomer, customer);
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            customer.setUpdateId(userId);
            customerService.saveOrUpdateCustomer(customer);
        } catch (DuplicateKeyException e) {
            LoggerUtils.error(getClass(), "[rCustomer editCustomer]", e);
            return ServerResponse.error(ResponseCode.CUSTOMER_EXISTS);
        }
        return ServerResponse.success();
    }

    @ApiOperation(value = "查询客户信息", notes = "查询客户信息")
    @GetMapping(value = "/queryCustomer")
    public ServerResponse<CustomerResponse> queryCustomer() {
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        RCustomer customer = customerService.queryCustomerInfo(userId);
        CustomerResponse customerResponse = new CustomerResponse();
        if (customer !=null){
            BeanUtils.copyProperties(customer, customerResponse);
            customerResponse.setBookingTime(customer.getStartTime());
        }
        return ServerResponse.success(customerResponse);
    }


}
